<?php
/**
 * 功能描述：此接口用于发送短信内容到指定的手机
 * 
 * 作者：zenan.li
 * 
 * Created by Zend Studio.
 * User: zenan.li (FDG348)
 * Date: 2017/9/5
 * Time: 15:39
 */

namespace common\helps;

use Yii;
use backend\models\LogSms;

require('api_sms/include/Client.php');

class api_sms{
    /**
     *  add by zenan.li (FDG348) 20170905
     */
    public function sendSms($mobiles,$content){
        if (Yii::$app->params['sms_switch'] == 'off') {
            return true;
        }
        if (!is_array($mobiles) && count($mobiles) == 0) {
            return false;
        }
        if ($content == '') {
            return false;
        }
        //连接客户端
        $client = new \Client(GW_URL, SERIAL_NUMBER, SMS_PASSWORD,SMS_SESSION_KEY);
        //设置发送短信内容的字符集
        $client->setOutgoingEncoding("UTF-8");
        //短信发送
        $statusCode = $client->sendSMS($mobiles,$content);
        //判断返回结果对应的错误代码将结果储存到数据库
        $sms_status = 1;
        $fail_content = '';
        if (intval($statusCode) !== 0){
            $sms_status = 2;
            $fail_content = $statusCode.':'.$GLOBALS['_SMS_ERROR_MESSAGE'][$statusCode];
        }
        if (Yii::$app->params['sms_log_switch'] == 'on'){
            foreach ($mobiles as $mobile){
                $logsms = new LogSms();
                $logsms->mobile = $mobile;
                $logsms->sms_content = $content;
                $logsms->sms_status = $sms_status;
                $logsms->ip = $_SERVER['REMOTE_ADDR'];
                if ($fail_content){
                    $logsms->fail_content = $fail_content;
                }
                $logsms->creator = $logsms->getCreator();
                $logsms->create_time = date('Y-m-d H:i:s',time());
                $logsms->insert();
            }
        }

        return true;
    }
}